package com.fernfx.xingtan.main.presenter;

import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseFragment;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.main.contract.MainContract;
import com.fernfx.xingtan.main.entity.ContactsInfoEntity;
import com.fernfx.xingtan.main.entity.PersonEntity;
import com.fernfx.xingtan.main.model.MainModel;
import com.fernfx.xingtan.utils.CollectionUtil;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.FragmentFactoryUtil;
import com.fernfx.xingtan.utils.LogUtils;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.List;
import java.util.Map;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

/**
 * main presenter
 */

public class MainPresenter<P extends MainContract.View> implements MainContract.Presenter {
    private P P;
    private MainContract.Model mainModel;
    private BaseFragment currentFragment;

    @Override
    public void init(BaseView P) {
        this.P = (P) P;
        mainModel = new MainModel();
    }



    @Override
    public void request(Map<String, Object> requestArgsMap) {

    }

    @Override
    public void detachView() {
        if (mainModel != null) {
            mainModel = null;
        }
    }

    @Override
    public void loadSelectFragment(int index) {
        FragmentManager mFragmentManager = P.getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        for (int i = 0; i < Constant.ApplicationVariable.TAB_INDEX; i++) {
            if (null != mFragmentManager) {
                Fragment fragment = mFragmentManager.findFragmentByTag(i + "");
                if (fragment != null) {
                    fragmentTransaction.remove(fragment);
                }
            }
        }
        //当前执行的fragment
        BaseFragment baseFragment = FragmentFactoryUtil.createFragment(index);
        if (currentFragment == null) {
            fragmentTransaction.add(R.id.fragment_rlt, baseFragment).commit();
            currentFragment = baseFragment;
        }
        if (currentFragment != baseFragment) {
            // 先判断是否被add过
            if (!baseFragment.isAdded()) {
                // 隐藏当前的fragment，add下一个到Activity中
                fragmentTransaction.hide(currentFragment)
                        .add(R.id.fragment_rlt, baseFragment).commit();
            } else {
                // 隐藏当前的fragment，显示下一个
                fragmentTransaction.hide(currentFragment).show(baseFragment)
                        .commit();
            }
            currentFragment = baseFragment;
        }

//        fragmentTransaction.replace(R.id.fragment_rlt, baseFragment).commitAllowingStateLoss();
    }
}
